/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JIFBuscarProductoPedido.java
 *
 * Created on 17-nov-2011, 17:49:14
 */
package jam.sfcc.application;

import jam.sfcc.business.CategoriaBusiness;
import jam.sfcc.business.ProductoBusiness;
import jam.sfcc.common.domain.Categoria;
import jam.sfcc.common.domain.Producto;
import jam.sfcc.common.domain.Vendedor;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Josue
 */
public class JIFBuscarProductoPedido extends javax.swing.JInternalFrame {
    private Vendedor vendedor;
    private DefaultTableModel dtm;
    private LinkedList<Categoria> categorias;
    private final int EXITO=1,FALLO=2;
    /** Creates new form JIFBuscarProductoPedido */
    public JIFBuscarProductoPedido(Vendedor vendedor) {
        initComponents();
        
        this.vendedor=vendedor;
        dtm=new DefaultTableModel(new String[]{"Descripción","Cantidad Total","Precio","Cantidad Requerida"}, 0);
        jtDetallesOrden.setModel(dtm);
        int resultado=EXITO;
        CategoriaBusiness categoriaBusiness=new CategoriaBusiness(vendedor.getNombre(), vendedor.getClave());
        try {
            categorias=categoriaBusiness.getCategorias();
            for (Categoria categoria : categorias) {
                
                jcbCategoria.addItem(categoria);
                
            }
        } catch (SQLException ex) {
            resultado=FALLO;
        }
        if(resultado==FALLO){
            JOptionPane.showMessageDialog(this, "Hubo un error.");
        }
        jtfNombreProducto.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jtDetallesOrden = new javax.swing.JTable();
        panelEncabezados1 = new jam.sfcc.application.PanelEncabezados();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfNombreProducto = new javax.swing.JTextField();
        jcbCategoria = new javax.swing.JComboBox();
        jbAgregar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlProductos = new javax.swing.JList();

        setClosable(true);
        setIconifiable(true);
        setTitle("Agregar Productos");

        jtDetallesOrden.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jtDetallesOrden);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Agregar Productos");

        javax.swing.GroupLayout panelEncabezados1Layout = new javax.swing.GroupLayout(panelEncabezados1);
        panelEncabezados1.setLayout(panelEncabezados1Layout);
        panelEncabezados1Layout.setHorizontalGroup(
            panelEncabezados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelEncabezados1Layout.createSequentialGroup()
                .addContainerGap(216, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(226, 226, 226))
        );
        panelEncabezados1Layout.setVerticalGroup(
            panelEncabezados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEncabezados1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel2.setText("Digite el nombre del producto:");

        jtfNombreProducto.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jtfNombreProducto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtfNombreProductoKeyTyped(evt);
            }
        });

        jbAgregar.setText("Agregar");

        jbCancelar.setText("Cancelar");

        jlProductos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(jlProductos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelEncabezados1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 486, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jbAgregar)
                        .addGap(18, 18, 18)
                        .addComponent(jbCancelar)
                        .addGap(32, 32, 32))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 682, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfNombreProducto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))
                .addGap(64, 64, 64)
                .addComponent(jcbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(84, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelEncabezados1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfNombreProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(57, 57, 57)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAgregar)
                    .addComponent(jbCancelar))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfNombreProductoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfNombreProductoKeyTyped
       // jPopupMenu1.removeAll();
        LinkedList<Producto> productos=new LinkedList<>();
        ProductoBusiness productoBusiness=new ProductoBusiness(vendedor.getNombre(), vendedor.getClave());
        try {
            productos=productoBusiness.getProductosPorCategoria(((Categoria)jcbCategoria.getSelectedItem()).getCodCategoria());
            
            
                jlProductos.setListData(productos.toArray());
           
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
         
        
    }//GEN-LAST:event_jtfNombreProductoKeyTyped

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JComboBox jcbCategoria;
    private javax.swing.JList jlProductos;
    private javax.swing.JTable jtDetallesOrden;
    private javax.swing.JTextField jtfNombreProducto;
    private jam.sfcc.application.PanelEncabezados panelEncabezados1;
    // End of variables declaration//GEN-END:variables
}
